package com.blogs.ui;


import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blogs.component.BaseActivity;
import com.blogs.entity.BlogDraftFeed;
import com.blogs.tools.DBSharedPreferences;
import com.blogs.tools.TbBlogDraft;
import com.cnblogs.R;
import com.cnblogs.R.color;

public class BlogPublicActivity extends BaseActivity implements OnClickListener{
	private static final int SHOW_SUBACTIVITY=1;
	private DBSharedPreferences sp;
	private RelativeLayout title_top_bg;
	private Button title_include_back;
	private TextView title_include_title;
	private Button title_blog_save;
	private RelativeLayout blog_public_buttom_ll;
	private RelativeLayout blog_public_bg;
	private Button blog_public_send_btn;
	private EditText blog_public_et;
//	private InputMethodManager imm;
	
	private int draft_id=0;
	private String txbTitle="";
	private int name_site_categroy_id=-1;
	private boolean cbHomeCandidate=false;
	private boolean cbIsPublishToSiteHome=false;
	private boolean chkDisplayHomePage=false;
	private String txbExcerpt="";
	private String txbTag="";
	private Toast toast;
//	private ArrayList<CateFeed > cateList;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.blog_pubilc);
		sp= new DBSharedPreferences(this);
		if (sp.GetSessionID().equals("")) {
			this.goToLogin();
			this.finish();
			return;
		}
//		s = (InputMethodManager)getSystemService(this.INPUT_METHOD_SERVICE);//软键盘
		ViewGroup headview = (ViewGroup) findViewById(R.id.main_title_include);
		title_top_bg=(RelativeLayout)headview;
		title_include_title = (TextView) headview.findViewById(R.id.title_include_title);
		title_include_back = (Button) headview.findViewById(R.id.title_include_back);
		title_include_title.setText("发博文");
		title_include_back.setVisibility(View.VISIBLE);
		title_include_back.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		title_blog_save=(Button)headview.findViewById(R.id.title_blog_save);
		title_blog_save.setVisibility(View.VISIBLE);
		blog_public_buttom_ll=(RelativeLayout)findViewById(R.id.blog_public_buttom_ll);
		blog_public_bg=(RelativeLayout)findViewById(R.id.blog_public_bg);
		blog_public_send_btn=(Button)findViewById(R.id.blog_public_send_btn);
		blog_public_et=(EditText)findViewById(R.id.blog_public_et);
		blog_public_send_btn.setOnClickListener(this);
		title_blog_save.setOnClickListener(this);
		
//		cateList=ListDate.getCatelList();
		this.InitSkin();
		this.InitValue();
	}
	private void InitValue()
	{
		draft_id=getIntent().getIntExtra("id", 0);
		txbTitle=getIntent().getStringExtra("txbTitle");
		name_site_categroy_id=getIntent().getIntExtra("name_site_categroy_id", -1);
		txbTag=getIntent().getStringExtra("txbTag");
		txbExcerpt=getIntent().getStringExtra("txbExcerpt");
		cbHomeCandidate=getIntent().getBooleanExtra("cbHomeCandidate", false);
		cbIsPublishToSiteHome=getIntent().getBooleanExtra("cbIsPublishToSiteHome", false);
		chkDisplayHomePage=getIntent().getBooleanExtra("chkDisplayHomePage", false);
		blog_public_et.setText(getIntent().getStringExtra("EditorBody"));
		
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) 
		{
			this.finish();
		}
		else if(resultCode==Activity.RESULT_CANCELED)
		{
			txbTitle=data.getStringExtra("txbTitle");
			name_site_categroy_id=data.getIntExtra("name_site_categroy_id", -1);
			txbTag=data.getStringExtra("txbTag");
			txbExcerpt=data.getStringExtra("txbExcerpt");
			cbHomeCandidate=data.getBooleanExtra("cbHomeCandidate", false);
			cbIsPublishToSiteHome=data.getBooleanExtra("cbIsPublishToSiteHome", false);
			chkDisplayHomePage=data.getBooleanExtra("chkDisplayHomePage", false);
		}
	}
	private void InitSkin()
	{
		if(sp.GetIsDarkState())
		{
			title_top_bg.getBackground().setLevel(2);
			blog_public_bg.getBackground().setLevel(2);
			blog_public_send_btn.getBackground().setLevel(2);
			blog_public_buttom_ll.getBackground().setLevel(2);
			blog_public_et.setTextColor(getResources().getColor(color.title_text_night));
		}
		else
		{
			title_top_bg.getBackground().setLevel(1);
			blog_public_bg.getBackground().setLevel(1);
			blog_public_send_btn.getBackground().setLevel(1);
			blog_public_buttom_ll.getBackground().setLevel(1);
			blog_public_et.setTextColor(getResources().getColor(color.title_text_light));
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==blog_public_send_btn)
		{
//			imm.hideSoftInputFromWindow(blog_public_et.getWindowToken(), 0);
			if(blog_public_et.length()<20)
			{
				this.showToast("亲,文章也太短了吧!");
				return;
			}
			Intent intent = new Intent(this, SubBlogPublicActivity.class);
			intent.putExtra("draft_id", draft_id);
			intent.putExtra("txbTitle", txbTitle);
			intent.putExtra("EditorBody", blog_public_et.getText().toString());
			intent.putExtra("name_site_categroy_id",name_site_categroy_id);
			intent.putExtra("cbHomeCandidate", cbHomeCandidate);
			intent.putExtra("cbIsPublishToSiteHome", cbIsPublishToSiteHome);
			intent.putExtra("chkDisplayHomePage", chkDisplayHomePage);
			intent.putExtra("txbExcerpt", txbExcerpt);
			intent.putExtra("txbTag", txbTag);
			startActivityForResult(intent, SHOW_SUBACTIVITY);
		}
		else if(v==title_blog_save)
		{
			if(blog_public_et.length()<5)
			{
				this.showToast("亲,那么简短,不用存草稿了吧!");
				return;
			}
			TbBlogDraft tb=new TbBlogDraft(this);
			if(draft_id==0)
			{
				BlogDraftFeed feed=new BlogDraftFeed();
				feed.txbTitle=txbTitle;
				feed.EditorBody=blog_public_et.getText().toString();
				feed.name_site_categroy_id=name_site_categroy_id;
				feed.txbTag=txbTag;
				feed.txbExcerpt=txbExcerpt;
				feed.cbHomeCandidate=cbHomeCandidate;
				feed.cbIsPublishToSiteHome=cbIsPublishToSiteHome;
				feed.chkDisplayHomePage=chkDisplayHomePage;
				feed.err_text="存草稿";
				draft_id=tb.InsertGetId(feed);
				
			}
			else
			{
				ContentValues values=new ContentValues();
				values.put("txbTitle", txbTitle);
				values.put("EditorBody", blog_public_et.getText().toString());
				values.put("name_site_categroy_id", name_site_categroy_id);
				values.put("cbHomeCandidate", cbHomeCandidate);
				values.put("cbIsPublishToSiteHome", cbIsPublishToSiteHome);
				values.put("chkDisplayHomePage", chkDisplayHomePage);
				values.put("txbExcerpt", txbExcerpt);
				values.put("txbTag", txbTag);
				values.put("err_text", "存草稿");
				tb.Update(draft_id+"", values);
			}
			tb.DbClose();
			Intent intent = new Intent("BlogDraftReceiver");
			sendBroadcast(intent);//发送广播
			showToast("保存草稿成功");
		}
	}
	public void goToLogin() {

		Intent intent = new Intent();
		intent.setClass(this, UserLoginActivity.class);
		startActivity(intent);
	}
	private void showToast(String text) {
		if (toast == null) {
			toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
		} else {
			toast.setText(text);
			toast.setDuration(Toast.LENGTH_SHORT);
		}
		toast.show();
	}
}
